package com.haoyun.common.utils;

import com.baomidou.mybatisplus.toolkit.IOUtils;
import com.google.common.collect.Maps;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties;

/**
 * Created by twg on 2018/5/30.
 */
public final class PropertiesUtil {

    private static Map<Object, Object> propsMap = Maps.newConcurrentMap();

    /**
     * 通过输入流加载配置文件
     *
     * @param fin
     * @return
     * @throws IOException
     */
    public static Properties loadPropertiesFromInputStream(InputStream fin) throws IOException {
        Properties props = new Properties();
        InputStreamReader reader = new InputStreamReader(fin, Charset.defaultCharset());
        try {
            props.load(reader);
            return props;
        } finally {
            IOUtils.closeQuietly(reader, fin);
        }
    }

    /**
     * 读取配置文件属性到map
     *
     * @param props
     */
    public static Map loadProperties(Properties props) {
        for (Map.Entry<Object, Object> entry : props.entrySet()) {
            String key = (String) entry.getKey();
            Object value = entry.getValue();
            if (key != null && value != null) {
                propsMap.put(key, value);
            }
        }
        return propsMap;
    }

}
